package interfaces;

import java.util.ArrayList;
import java.util.List;

import negocio.Conta;

import objetos.AdapterListView;
import objetos.ItemListView;

import android.app.Activity;

import com.example.money.R;


import databases.DsConta;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class PrincipalActivity extends Activity implements OnItemClickListener {
	Button buttonCadastros;
	Button buttonLancamento;
	ImageButton buttonCreditos;
	
	ListView listView;
    private AdapterListView adapterListView;
	DsConta dbDataSource;
    private ArrayList<ItemListView> itens;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);
		addListenerOnButton();
		montaListView();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.cadastros, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
			case R.id.cadastros_categorias:
				startActivity(new Intent(PrincipalActivity.this, CategoriasActivity.class));
				return true;
				
			case R.id.cadastros_contas:
				startActivity(new Intent(PrincipalActivity.this, ContaActivity.class));
				return true;
				
			case R.id.cadastros_favorecidos:
				startActivity(new Intent(PrincipalActivity.this, FavorecidoActivity.class));
				return true;
		
			default:
				return super.onContextItemSelected(item);
		}
	}

	public void addListenerOnButton() {
		buttonCadastros = (Button) findViewById(R.id.bCadastros);
		buttonLancamento = (Button) findViewById(R.id.bLancamento);
		buttonCreditos = (ImageButton) findViewById(R.id.imageButtonCreditos);
		
		registerForContextMenu(buttonCadastros);
		
		buttonCadastros.setOnClickListener(new OnClickListener() {
 			@Override
			public void onClick(View arg0) {
 				openContextMenu(arg0);
			}
 
		});
		
		buttonLancamento.setOnClickListener(new OnClickListener() {
 			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(PrincipalActivity.this, LancamentoActivity.class));
			}
 
		});
		
		buttonCreditos.setOnClickListener(new OnClickListener() {
 			@Override
			public void onClick(View arg0) {
 				startActivity(new Intent(PrincipalActivity.this, CreditosActivity.class));
			}
 		});
	}
	
	@Override
	public void onRestart()
	{
		super.onRestart();
		
		montaListView();
	}

	private void montaListView(){
		dbDataSource = new DsConta(this);
		dbDataSource.open();

		listView = (ListView)findViewById(R.id.lvwSaldoContas);
        //Define o Listener quando alguem clicar no item.
        listView.setOnItemClickListener(this);

		List<Conta> values = dbDataSource.getAll();
        itens = new ArrayList<ItemListView>();
        ItemListView cadaItem;
        
        for (Conta conta : values) {
			cadaItem = new ItemListView(conta.getNome());
			itens.add(cadaItem);
		}
		
        //Cria o adapter
        adapterListView = new AdapterListView(this, itens);

        //Define o Adapter
        listView.setAdapter(adapterListView);
        //Cor quando a lista Ã© selecionada para ralagem.
        listView.setCacheColorHint(Color.TRANSPARENT);
	}
	
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        //Pega o item que foi selecionado.
        ItemListView item = adapterListView.getItem(arg2);
        //DemostraÃ§Ã£o
        Toast.makeText(this, "Você Clicou em: " + item.getTexto(), Toast.LENGTH_LONG).show();
    }

}
